import json

from pyecharts import options as opts
from pyecharts.charts import Tree
import pymongo
from bson.json_util import dumps
import json
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["spider-db"]
collection = db["wzj_category"]
result = collection.find({})  # bjson格式的数据
json_str = dumps(result)  # 将数据库查询结果转为json字符串
json_obj = json.loads(json_str)  # 将json字符串转为python的数据对象

data = [
    {
        "name": "筛选分类",
        "children": json_obj,
    }
]
c = (
    Tree()
    .add("", data)
    .set_global_opts(title_opts=opts.TitleOpts(title="Tree-基本示例"))
    .render("tree_category.html")  # 生成一个展示可视化图表的html
)
